package bai.publicwelfare_backend.mapper.activity;

import bai.publicwelfare_backend.pojo.entity.activity.Badge;
import bai.publicwelfare_backend.pojo.entity.activity.CharityEvent;
import bai.publicwelfare_backend.pojo.entity.activity.EventParticipant;
import bai.publicwelfare_backend.pojo.entity.activity.UserBadge;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityMapper {
    // 发布活动
    @Insert("INSERT INTO charity_events(title, description, start_time, location, max_participants, organizer_id, created_at, status) " +
            "VALUES(#{title},#{description},#{startTime},#{location},#{maxParticipants}," +
            "#{organizerId},#{createdAt},#{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void release(CharityEvent charityEvent);
//    参加活动
    @Insert("INSERT INTO event_participants(event_id, user_id, joined_at) " +
            "VALUES(#{eventId},#{userId},#{joinedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void attend(EventParticipant eventParticipant);
    // 查询活动详情
    @Select("SELECT * FROM charity_events WHERE id = #{eventId}")
    CharityEvent selectEventById(Long eventId);

    // 检查参与记录
    @Select("SELECT COUNT(*) > 0 FROM event_participants " +
            "WHERE event_id = #{eventId} AND user_id = #{userId}")
    boolean isParticipated(@Param("eventId") Long eventId,
                           @Param("userId") Long userId);
@Update("UPDATE charity_events SET actual_participants = actual_participants + 1 WHERE id = #{eventId}")
   void incrementActualParticipants(Long eventId);
// 查询活动列表
@Select("SELECT * FROM charity_events ORDER BY created_at DESC") // 建议添加排序
List<CharityEvent> selectAllEvents();

//查询勋章
@Select("SELECT * FROM badge_definition where id = #{id}")
Badge selectBadge(Integer id);

//查询勋章获得列表
@Select("SELECT * FROM user_badge where user_id = #{userId}")
List<UserBadge> selectUserBadge(Long userId);

    // 扣除用户积分
    @Update("UPDATE user SET points = points - #{points} WHERE id = #{userId}")
    void deductUserPoints(@Param("userId") Long userId, @Param("points") Integer points);

    // 添加用户勋章记录
    @Insert("INSERT INTO user_badge(user_id, badge_id) VALUES(#{userId}, #{badgeId})")
    void addUserBadge(@Param("userId") Long userId, @Param("badgeId") Integer badgeId);
}
